package com.qqt.csr.order.assembler;

import cn.hutool.core.bean.BeanUtil;
import com.qqt.csr.order.entity.OrderDynamic;
import com.qqt.csr.order.vo.resp.OrderDynamicRespVO;

import java.util.Collections;
import java.util.List;

/**
 * @Author: yang hao
 * @Date: 2024/12/12 9:38
 * @Description: 工单动态装配者
 **/
public class OrderDynamicAssembler {

    private static final String NULL_PARENT_CONTENT = "该评论已被删除";

    /**
     * 工单动态装配
     * @param dynamics {@link List<OrderDynamic>} 工单动态
     * @param parentDynamics {@link List<OrderDynamic>} 工单父动态
     * @return {@link List<OrderDynamicRespVO>} 工单动态响应
     */
    public static List<OrderDynamicRespVO> convert(List<OrderDynamic> dynamics, List<OrderDynamic> parentDynamics) {
        // 若动态为空，则返回空集合
        if (dynamics == null || dynamics.isEmpty()) {
            return Collections.emptyList();
        }
        return dynamics.stream().map(dynamic -> {
            // 获取父级动态
            OrderDynamic parentDynamic = parentDynamics.stream().filter(parent -> parent.getId().equals(dynamic.getParentId())).findFirst().orElse(null);
            // 动态装配
            return convert(dynamic, parentDynamic);
        }).toList();
    }

    /**
     * 动态装配
     * @param dynamic {@link OrderDynamic} 动态
     * @param parentDynamic {@link OrderDynamic} 父动态
     * @return {@link OrderDynamicRespVO} 动态响应
     */
    public static OrderDynamicRespVO convert(OrderDynamic dynamic, OrderDynamic parentDynamic) {
        OrderDynamicRespVO response = BeanUtil.copyProperties(dynamic, OrderDynamicRespVO.class);
        // 动态是否存在父级动态
        if (dynamic.getParentId() != null && dynamic.getParentId() > 0) {
            if (parentDynamic != null) {
                // 父级动态存在
                response.setParentContent(parentDynamic.getContent());
                response.setExistParent(0);
            } else {
                // 父级动态不存在
                response.setParentContent(NULL_PARENT_CONTENT);
                response.setExistParent(1);
            }
        }
        return response;
    }

}
